import { styleSizeUnit } from "@/decoration/utils/style"

export default {
  // #ifdef MP-WEIXIN
  // 将自定义节点设置成虚拟的，更加接近Vue组件的表现，能更好的使用flex属性
  options: {
    virtualHost: true,
    multipleSlots: true,
    styleIsolation: "shared"
  },
  // #endif
  props: {
    data: {
      type: Object,
      default: () => ({})
    },
    from: {
      type: Object,
      default: () => ({})
    },
    params: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      mitt: null,
      decoWrap: null,
      decoEntryCommon: null
    }
  },
  computed: {
    innerStyle() {
      return styleSizeUnit(this.data?.innerStyle)
    }
  },
  created() {
    this.useEntry()
    this.useWrap()
  },
  mounted() {},
  methods: {
    useEntry() {
      const name = this.$options.name
      if (name === "deco-entry-common") return
      this.decoEntryCommon = this.useParent("deco-entry-common")
      if (this.decoEntryCommon) {
        this.mitt = this.decoEntryCommon?.mitt
      }
    },
    useWrap() {
      const name = this.$options.name
      if (name === "deco-wrap") return
      this.decoWrap = this.useParent("deco-wrap")
      if (this.decoEntryCommon) {
        this.mitt = this.decoEntryCommon?.mitt
      }
    },
    // 获取指定名称父级实例
    useParent(name) {
      // 获取当前实例的父级
      let parent = this.$parent
      // 获取父级名称
      let parentName = parent.$options.name
      // 循环检查父级名称是否与传入的name参数相等
      while (parentName !== name) {
        // 获取父级父级
        parent = parent.$parent
        // 检查父级是否存在
        if (!parent) return null
        // 获取父级名称
        parentName = parent.$options.name
      }
      // 返回父级
      return parent
    },
    // 获取元素尺寸
    useRect(selector, all) {
      return new Promise((resolve) => {
        uni
          .createSelectorQuery()
          .in(this)
          [all ? "selectAll" : "select"](selector)
          .boundingClientRect((rect) => {
            if (all && Array.isArray(rect) && rect.length) {
              resolve(rect)
            }
            if (!all && rect) {
              resolve(rect)
            }
          })
          .exec()
      })
    }
  }
}
